function d = depfixdb(cost, salvage, life, period, month)
%DEPFIXDB Fixed declining-balance depreciation.
%
%   D = depfixdb(COST, SALVAGE, LIFE, PERIOD)
%   D = depfixdb(COST, SALVAGE, LIFE, PERIOD, MONTH)
%
%   Optional Inputs: MONTH
%
%   Inputs:
%      COST - Scalar for the initial value of the asset.
%
%   SALVAGE - Scalar for the salvage value of the asset.
%
%      LIFE - Scalar value for the life of the asset in years.
%
%    PERIOD - Scalar integer for the number of periods.
%
%     MONTH - Scalar value for the number months in the first year of the life
%             of the asset.  The default is 12.
%
%   Outputs:
%   D - Scalar value of the depreciation
%
%
%   Example:
%      A car is purchased for $11,000 with a salvage value $1500 and a lifetime
%      of eight years. To calculate the depreciation for the first five years:
%
%      d = depfixdb(11000, 1500, 8, 5)
%
%      format bank
%
%      d = 2425.08 1890.44 1473.67 1148.78 895.5
%
%   See also DEPGENDB, DEPRDV, DEPSOYD, DEPSTLN.

%   Copyright 1995-2006 The MathWorks, Inc.
%   $Revision: 1.6.2.2 $   $Date: 2006/05/17 20:51:39 $

% Input validation
if nargin < 5
    month = 12;  % Default MONTH is 12
end

if nargin < 4
    error('finance:depfixdb:tooFewInputs', ...
        'Too few inputs.')
end

if numel(cost) > 1 || isempty(cost)
    error('finance:depfixdb:invalidCost', ...
        'COST must be scalar value.')
end

if numel(salvage) > 1 || isempty(salvage)
    error('finance:depfixdb:invalidSalvage', ...
        'SALVAGE must be scalar value.')
end

if numel(life) > 1 || isempty(life)
    error('finance:depfixdb:invalidLife', ...
        'LIFE must be scalar value.')
end

if numel(period) > 1 || mod(period, 1) ~= 0 || isempty(period)
    error('finance:depfixdb:invalidPeriod', ...
        'PERIOD must be scalar integer value.')
end

if numel(month) > 1
    error('finance:depfixdb:invalidMonth', ...
        'MONTH must be scalar value.')
end

if isempty(month)
    month = 12;  % Default MONTH is 12
end

% Determine depreciation rate
rate = 1-((salvage./cost).^(1./life));

d(1) = cost.*rate.*month/12;    % RDV after first period
if abs(d(1) - cost-salvage) < 1e-6
    d(2) = [];

else
    d(2) = (cost-d(1)).*rate;   % RDV after second period
    n = 3:period-1;             % Variation in length of n prohibits vectorization
    d(n) = (1-rate).^(n-2)*d(2);
end

if month == 12
    month = 0;
end

% Determine depreciable value remaining after last period
if sum(d) >= cost-salvage
    d(period) = 0;

else
    d(period) = ((cost-sum(d))*rate*(12-month))/12;
end


% [EOF]
